package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.PagePreserveDao;
import com.jyxd.web.data.basic.PagePreserve;
import com.jyxd.web.data.dto.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
public class PagePreserveService {

    @Autowired
    private PagePreserveDao pagePreserveDao;

    /**
     * 新增或者修改保存起始页码
     *
     * @param pagePreserve
     * @return
     */
    public HttpResult insert(PagePreserve pagePreserve) {
        errorIfEmpty(pagePreserve, "数据不存在");
        errorIfEmpty(pagePreserve.getPatientId(), "患者id不存在");
        //errorIfEmpty(pagePreserve.getFormKey(), "表单key不存在");
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", pagePreserve.getPatientId());
        map.put("formKey", pagePreserve.getFormKey());
        map.put("stutas", 1);
        PagePreserve oldPagePreserve = pagePreserveDao.queryByPatientIdAndFormKey(map);
        if (Objects.nonNull(oldPagePreserve)) {
            oldPagePreserve.setPageNum(pagePreserve.getPageNum());
            pagePreserveDao.update(oldPagePreserve);
            return new HttpResult(200, "数据修改成功");
        }
        pagePreserve.setId(getUUID());
        pagePreserve.setStatus(1);
        pagePreserve.setCreateTime(new Date());
        pagePreserveDao.insert(pagePreserve);
        return new HttpResult(200, "数据新增成功");
    }

    /**
     * 查询起始页码
     *
     * @param map
     * @return
     */
    public HttpResult queryList(Map<String, Object> map) {
        errorIfEmpty(map, "参数数据不存在");
        errorIfEmpty(map.get("patientId"), "患者id不存在");
        return new HttpResult(200, pagePreserveDao.queryList(map), "数据查询成功");
    }

}
